package com.example.springboot.controller;

import com.example.springboot.common.Result;

import com.example.springboot.entity.Companies;
import com.example.springboot.entity.students;
import com.example.springboot.entity.user;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result addUser(@RequestBody user user) {
        userService.addUser(user);
        return Result.success("添加成功");
    }

    @PostMapping("/addBoss")
    public Result addBoss(@RequestBody Companies companies) {
        userService.addBoss(companies);
        return Result.success("添加成功");
    }
    @PostMapping("/addStu")
    public Result addStu(@RequestBody students student) {
        userService.addStu(student);
        return Result.success("添加成功");
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<user> userList = userService.findAll();
        return Result.success(userList);
    }

    @GetMapping("/selectStuAll")
    public Result selectStuAll() {
        List<students> student = userService.selectStuAll();
        return Result.success(student);
    }

    @GetMapping("/selectBossAll")
    public Result selectBossAll() {
        List<Companies> companiesList = userService.selectBossAll();

        return Result.success(companiesList);
    }

    @PostMapping("/update")
    public Result updateUserByUsername(@RequestBody user user) {
        userService.updateUserByUsername(user);
        return Result.success("修改成功");
    }
    @PutMapping("/edit")
    public Result editUserByUsername(@RequestBody user user) {
        userService.editUserByUsername(user);
        return Result.success("编辑成功");
    }

    @DeleteMapping("deleteUser/{user_id}")
    public String deleteUser(@PathVariable int user_id) {
        userService.deleteUser(user_id);
        return "User record deleted successfully!";
    }

    @DeleteMapping("deleteBossUser/{user_id}")
    public String deleteBossUser(@PathVariable int user_id) {
        userService.deleteBossUser(user_id);
        return "User record deleted successfully!";
    }

    @DeleteMapping("deleteStuUser/{user_id}")
    public String deleteStuUser(@PathVariable int user_id) {
        userService.deleteStuUser(user_id);
        return "User record deleted successfully!";
    }

    @DeleteMapping("/deleteBatch")
    public Result deleteAccountBatch(@RequestBody List<Long> ids) {
        try {
            userService.deleteAccountBatch(ids);
            return Result.success("批量删除成功");
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/selectByUsername")
    public Result selectByName(@RequestParam String username) {
        return Result.success(userService.selectByName(username));
    }
}
